import {
  addProgrammeInfo,
  deleteProgrammeById,
  exportProgramme,
  getProgrammeByProgrammeId,
  getProgrammeList,
  getSelectedTasksAndExaminationIdsByProgrammeId,
  modifyProgrammeInfo,
} from "@/service/modules/programme";
import { createAsyncThunk, createSlice, isAction } from "@reduxjs/toolkit";

interface programmeState {
  programmeList: any[];
  shareList: any;
  total: number;
  detailInfo: any;
}
const initialState: programmeState = {
  programmeList: [],
  total: 0,
  shareList: {},
  detailInfo: {},
};

export const fetchGetProgrammeList = createAsyncThunk(
  "Pragramme/list",
  async (query: { pageNum: number; pageSize: number }) => {
    const res = await getProgrammeList(query);
    // console.log(res);
    return res;
  }
);

export const fetchGetExaminationIdsAndTaskIdsById = createAsyncThunk(
  "programme/isShareIds",
  async (id: number) => {
    const res = await getSelectedTasksAndExaminationIdsByProgrammeId(id);
    // console.log(res);
    return res.data;
  }
);

export const fetchGetProgrammeDetailInfo = createAsyncThunk(
  "programme/detail",
  async (id: number) => {
    const res = await getProgrammeByProgrammeId(id);
    // console.log(res);

    return res.data;
  }
);

export const fetchModifyProgrammeInfoAction = createAsyncThunk(
  "programme/modify",
  async (body: {
    content?: string | null;
    createBy?: string | null;
    examinationIds: number[];
    id: number;
    illnessId?: number;
    programmeExaminationList?: any[];
    programmeExaminations?: {
      createBy: string;
      createTime: string;
      examinationId: number;
      id: number;
      programmeId: number;
      remark: null;
      updateBy: string;
      updateTime: string;
    }[];
    programmeTasks?: {
      createBy: string;
      createTime: string;
      taskId: number;
      id: number;
      programmeId: number;
      remark: null;
      updateBy: string;
      updateTime: string;
    }[];
    taskIds: number[];
    title?: string | null;
    updateBy?: string;
    updateTime?: string;
  }) => {
    const res = await modifyProgrammeInfo(body);
    console.log(res);

    return res;
  }
);

export const fetchModifyProgrammeAction = createAsyncThunk(
  "programme/modify",
  async (body: {
    content?: string;
    createBy?: string;
    examinationIds: number[];
    id: number | null;
    illnessId?: number;
    programmeExaminationList?: any[];
    programmeExaminations?: {
      createBy: string;
      createTime: string;
      examinationId: number;
      id: number;
      programmeId: number;
      remark: null;
      updateBy: string;
      updateTime: string;
    }[];
    programmeTasks?: {
      createBy: string;
      createTime: string;
      taskId: number;
      id: number;
      programmeId: number;
      remark: null;
      updateBy: string;
      updateTime: string;
    }[];
    taskIds: number[];
    title?: string;
    updateBy?: string;
    updateTime?: string;
  }) => {
    const res = await modifyProgrammeInfo(body);
    console.log(res);

    return res;
  }
);

export const fetchAddProgrammeInfo = createAsyncThunk(
  "programme/add",
  async (body: {
    content: string | null;
    createBy: null;
    creatTime: null;
    examinationIds: number[];
    id: null;
    illnessId: number;
    programmeExaminationList: any[];
    taskIds: number[];
    title: string | null;
    updateBy: null;
    updateTime: null;
  }) => {
    const res = await addProgrammeInfo(body);
    console.log(res);

    return res;
  }
);

export const fetchDeleteProgrammeByIdAction = createAsyncThunk(
  "rogramme/delete",
  async (ids: string[]) => {
    const res = await deleteProgrammeById(ids);
    console.log(res);
    return res;
  }
);

export const fetchExportProgrammeAction = createAsyncThunk(
  "programme/export",
  async (query: { pageSize: number; pageNum: number }) => {
    const res = await exportProgramme(query);
    console.log(res);
    return res;
  }
);

const programmeSlice = createSlice({
  name: "programme",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchGetProgrammeList.fulfilled, (state, action) => {
        state.programmeList = action.payload.rows;
        state.total = action.payload.total;
      })
      .addCase(
        fetchGetExaminationIdsAndTaskIdsById.fulfilled,
        (state, action) => {
          state.shareList = action.payload;
        }
      )
      .addCase(fetchGetProgrammeDetailInfo.fulfilled, (state, action) => {
        state.detailInfo = action.payload;
      });
  },
});
export default programmeSlice.reducer;
